package com.zomato.photofilters.imageprocessors.subfilters;

import com.zomato.photofilters.ResourceTable;
import com.zomato.photofilters.imageprocessors.SubFilter;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

/**
 * @author varun
 * Subfilter to add Vignette effect on an image
 */
public class VignetteSubFilter implements SubFilter {

    private static String tag = "";
    private Context context;

    // value of alpha is between 0-255
    private int alpha = 0;

    /**
     * Initialise Vignette subfilter
     *
     * @param alpha value of alpha ranges from 0-255 (Intensity of Vignette effect)
     */
    public VignetteSubFilter(Context context, int alpha) {
        this.context = context;
        this.alpha = alpha;
    }

    @Override
    public PixelMap process(PixelMap inputImage, boolean flag) {
        Image image = new Image(context.getApplicationContext());
        image.setPixelMap(ResourceTable.Graphic_vignette);
        PixelMap vignette = image.getPixelMap();

        int width = inputImage.getImageInfo().size.width;
        int height = inputImage.getImageInfo().size.height;

        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(width, height);
        vignette = PixelMap.create(vignette, initializationOptions);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setAlpha(alpha);

        Canvas comboImage = new Canvas();
        comboImage.drawPixelMapHolder(new PixelMapHolder(vignette), 0f, 0f, paint);
        return inputImage;
    }

    @Override
    public Object getTag() {
        return tag;
    }

    @Override
    public void setTag(Object tag) {
        VignetteSubFilter.tag = (String) tag;
    }

    /**
     * Change alpha value to new value
     */
    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

    /**
     * Changes alpha value by that number
     */
    public void changeAlpha(int value) {
        this.alpha += value;
        if (alpha > 255) {
            alpha = 255;
        } else if (alpha < 0) {
            alpha = 0;
        }
    }
}
